package com.example.loginproject.view.views

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.example.loginproject.R
import com.example.loginproject.tool.dp2px
import com.example.loginproject.tool.getMode
import com.example.loginproject.tool.getSize

class WaveAnimationView(context:Context,attrs:AttributeSet?):View(context, attrs) {
    private var mColor:Int
    private var mFirstWaveColor:Int
    private var mSecondWaveColor:Int
    private var mThirdWaveColor:Int
    private var mWavePace:Int
    private val mHeightrateArray= arrayListOf(0f,0f,0f)


    //默认颜色
    private val defaultColor = context.getColor(R.color.wave_green)
    private val mDefaultFast = 100
    private val mDefaultNormal = 500
    private val mDefaultSlow = 1000
    private var mWaveWidth = context.dp2px(50)//默认宽度
    private var mWaveHeight = context.dp2px(300)//默认高度
    private var mSpace = context.dp2px(20)//默认间距
    private var mDelay = 150L
    //实现事件回调
    private val callback:(()->Unit)? = null
    //创建画笔
    private val mPaint: Paint by lazy{
        Paint().apply{
            isAntiAlias = true
            color = defaultColor
            style = Paint.Style.FILL
        }
    }
    init{
        //解析属性
        context.obtainStyledAttributes(attrs, R.styleable.WaveAnimationView).apply {
        //解析颜色
            mColor = this.getColor(R.styleable.WaveAnimationView_color,defaultColor)
            mFirstWaveColor = this.getColor(R.styleable.WaveAnimationView_first_note_color,defaultColor)
            mSecondWaveColor = this.getColor(R.styleable.WaveAnimationView_second_note_color,defaultColor)
            mThirdWaveColor = this.getColor(R.styleable.WaveAnimationView_third_note_color,defaultColor)
            //解析动画速度
            val pace = this.getInteger(R.styleable.WaveAnimationView_pace,0)
            //1,2,3选择快慢
            //0是外部没有设值，默认为正常
            mWavePace = when(pace){
                0 ->mDefaultNormal
                1 -> mDefaultFast
                2 -> mDefaultNormal
                3 -> mDefaultSlow
                else-> pace
            }
            mDelay = (mWavePace*0.3).toLong()
            //回收
            recycle()
        }
        setOnClickListener {
            mCallback?.let{
                it(this)
            }
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        var mWidth = 0
        var mHeight = 0
        mWidth = when(getMode(widthMeasureSpec)){
            MeasureSpec.EXACTLY -> getSize(widthMeasureSpec)
            else -> (mWaveWidth*3+2*mSpace)
        }
        mHeight = when(getMode(heightMeasureSpec)){
            MeasureSpec.EXACTLY -> getSize(heightMeasureSpec)
            else -> mWaveHeight
        }
        setMeasuredDimension(mWidth,mHeight)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        //重新计算宽度，高度，间距
        if(height!=mWaveHeight){
            mWaveHeight = height
        }
        if(width !=  (mWaveWidth*3+2*mSpace)){
            mSpace = width /10
            mWaveWidth = (width - 2*mSpace)/3
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //画第一个音柱
        mPaint.color = mFirstWaveColor
        canvas.drawRect(0f,mWaveHeight*(1-mHeightrateArray[0]),mWaveWidth.toFloat(),mWaveHeight.toFloat(),mPaint)
        //画第二个
        mPaint.color = mSecondWaveColor
        canvas.drawRect((mWaveWidth+mSpace).toFloat(),mWaveHeight*(1-mHeightrateArray[1]),(mWaveWidth*2+mSpace).toFloat(),mWaveHeight.toFloat(),mPaint)
        mPaint.color = mThirdWaveColor
        canvas.drawRect((mWaveWidth*2+mSpace*2).toFloat(),mWaveHeight*(1-mHeightrateArray[2]),(mWaveWidth*3+2*mSpace).toFloat(),mWaveHeight.toFloat(),mPaint)
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        mAnimatorSet.start()
    }
    override fun onWindowVisibilityChanged(visibility: Int) {
        super.onWindowVisibilityChanged(visibility)
        if (visibility == View.INVISIBLE){
            mAnimatorSet.cancel()
        }
    }

    private val mAnimatorSet:AnimatorSet by lazy {
        val animators = arrayListOf<Animator>()
        for(i in 0 until 3){

            ValueAnimator.ofFloat(0f,1f).apply {
                duration = mWavePace.toLong()
                //循环无限次
                repeatCount =ValueAnimator.INFINITE
                //回弹
                repeatMode = ValueAnimator.REVERSE
                //延迟150ms
                startDelay =i*150L
                animators.add(this)
                addUpdateListener {
                    mHeightrateArray[i]=it.animatedValue as Float
                    invalidate()
                }
            }
    }
        AnimatorSet().apply {
            playTogether(animators)
        }
    }

    fun startAnimation(){
        mAnimatorSet.start()
    }
    fun pauseAnimation(){
        mAnimatorSet.pause()
    }
    fun stopAnimation(){
        mAnimatorSet.cancel()
    }
    //实现事件回调
    private var mCallback:((WaveAnimationView)->Unit)? = null
    fun addClickListener(callback:(WaveAnimationView)->Unit){
        mCallback = callback
    }
}